package com.hxbd.gxsec_data.controller;

import com.alibaba.fastjson.JSONObject;
import com.hxbd.gxsec_data.pojo.ImgDO;
import com.hxbd.gxsec_data.service.NewsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;
import java.util.Map;

@Controller
public class NewsController {
    @Autowired
    private NewsService newsService;

    @PostMapping(value = "/uploadCover")
    @ResponseBody
    public JSONObject uploadCover(@RequestParam MultipartFile file) {
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("link",newsService.uploadFiles(file,"cover").getUrl());
        return jsonObject;
    }

    @PostMapping(value = "/uploadNewsImg")
    @ResponseBody
    public JSONObject uploadNewsImg(@RequestParam MultipartFile file) {
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("link",newsService.uploadFiles(file,"newsContent").getUrl());
        return jsonObject;
    }

    @GetMapping(value = "/getAllNews")
    @ResponseBody
    public List<JSONObject> getAllNews(){
        return newsService.getAllNews();
    }

    @PostMapping(value = "/saveNews")
    @ResponseBody
    public boolean saveNews(@RequestParam Map<String,Object> map) {
        return newsService.addNewsData(map);
    }

    @PostMapping(value = "/saveUpdateNews")
    @ResponseBody
    public boolean saveUpdateNews(@RequestParam Map<String,Object> map) {
        return newsService.saveUpdateNews(map);
    }

    @PostMapping(value = "/batchNews")
    @ResponseBody
    public boolean batchNews(@RequestBody Map<String,Object> map) {
        return newsService.batchNews(map);
    }

    @PostMapping(value = "/batchUploadImg")
    @ResponseBody
    public boolean batchUploadImg(@RequestParam(value = "url") String url) {
        return newsService.batchUploadImg(url);
    }

    @GetMapping(value = "/getNewsByname")
    @ResponseBody
    public List<JSONObject> getNewsByname(@RequestParam String name){
        return  newsService.getNewsByname(name);
    }
}
